/**
 * Created by Administrator on 2015-6-14.
 */
class Model{
    public static pwarded:boolean = false;//人气
    public static rwarded:boolean = false;//掠夺

    public static referDate:Date = new Date();
    public static FIVE_MINUTES = 5 * 60 * 1000;
    public static ONE_MINUTES = 60 * 1000;
    public static DAY = 24 * 60 * 60 * 1000;
    public static TEN_MINUTES = 10000;
    public static EIGHT_HOUR = 1000 * 60 * 60 * 8;
    public static LastChatTime:number = 0;
    public static CurOrderId:number = -1;
    //装饰品位置
    //4 bg 5 nest 6 toy 7 frame 8 hat 9 glass 10 necklace
    public static DEC_BG = 4;
    public static DEC_NEST = 5;
    public static DEC_TOY = 6;
    public static DEC_FRAME = 7;
    public static DEC_HAT = 8;
    public static DEC_GLASS = 9;
    public static DEC_NECKLACE = 10;
    //buff类型
    public static TYPE_HAIR:number = 1;
    public static TYPE_Mood:number = 2;

    public static SYSTIME:number;

    public static MALE:number = 0;
    public static FEMALE:number = 1;

    public static GOOD_TYPE_MONEY:number = 100;
    public static GOOD_TYPE_CAT:number = 101;
    public static GOOD_TYPE_DOG:number = 102;
    public static GOOD_TYPE_ITEM:number = 103;
    public static GOOD_TYPE_PRIZE:number = 104;
    public static GOOD_TYPE_DIAMOND:number = 105;
    public static GOOD_TYPE_DECORATE:number = 106;
    public static GOOD_TYPE__GUILD_GOODS:number = 108;
    public static GOOD_TYPE_GUILD_MATERIAL:number = 109;
    public static GOOD_TYPE_GUILD_CAT:number = 110;
    public static GOOD_TYPE_GUILD_DOG:number = 111;

    public static prize:GoodInfo;
    public static PrizeBuyLimit:number = 3;
    public static worldRank:RankInfo[]=null;
    public static friendRank:RankInfo[]=null ;
    public static me:PlayerInfo = new PlayerInfo(null);
    public static props:PropInfo[] = [];
    public static dogs:GoodInfo[]=[];//todo 买猫的人优先展示猫 买狗的人优先展示狗
    public static cats:GoodInfo[]=[];
    public static goods:GoodInfo[]=[];
    public static guildGoods:GoodInfo[]=[];
    public static guildMaterial:GoodInfo[]=[];
    //public static guildCat:GoodInfo[]=[];
    //public static guildDog:GoodInfo[]=[];
    public static hideGoods:GoodInfo[]=[];
    public static goodsSorted:boolean = false;
    public static moneys:GoodInfo[] = [];
    public static decorators:GoodInfo[] = [];
    public static decorators4dog:number[]=[];
    public static decorators4cat:number[]=[];
    public static pets:Pet[] = [];//所有购买了的宠物
    public static diamonds:GoodInfo[]=[];
    public static strangerList:Pet[];
    public static datedList:number[] = [];
    public static robedList:number[] = [];
    public static attentionIdList:number[] = [];
    public static allAttentionIdList:number[] = [];//相互关注列表
    public static attentionList:any[];
    public static missionModels:Object;
    public static achieveModels:Object;
    public static unlockModels:Object;
    public static finishedMissions:number[];

    public static decModels:any[];
    public static decUnlockConfig:any[];
    public static currentDecorators:DecorateInfo[];
    public static petAttributes;
    public static myDecorates:DecorateInfo[];//my free decs

    public static TYPE_MISSION:number = 101;
    public static TYPE_ACHIVEMENT:number = 102;
    public static TYPE_UNLOCK:number = 103;
    public static curMissions:number[];
    public static curAchievements:number[];

    public static MailPage:number = 7;
    public static curMails:MailInfo[];
    public static mailMap;
    public static moreMails:boolean = true;

    public static UnlockConfigs:any[];
    public static signInfos:SignInfo[];
    public static totalSignCount:number=0;
    public static lastSignTime:number;
    public static resetDays:number=0;
    public static todaySigned:boolean = false;
    /** refereh cd time **/
    public static cdTime:number = 0;

    public static curGuideStep:number=0;
    public static MaxGuideStep:number=9;
    public static GuideType:string;
    public static GuideObserver:any;
    public static GuideCall:Function;
    public static GuideStarted:boolean = false;
    public static GuideNeedValue:number[];
    public static userExt:any;
    public static inDating:boolean = false;
    public static GLEAN_NOW:boolean = false;
    public static STOREHAIRS = 0;

    public static REVENGE_DATA;
    public static GuildDogCnt = 0;
    public static GuildCatCnt = 0;
    public static TotalGuildDogCnt = 0;
    public static TotalGuildCatCnt = 0;
    public static getStateFromMask(flag):boolean{
        var msk:string = Model.me.funMsk;
        if( msk == null || msk.length == 0) return false;
        return msk.charAt(flag-1) == "1";
    }

    public static getStateFromUMask(flag):boolean{
        var msk:string = Model.me.msk;
        if( msk == null || msk.length < flag) return false;
        return msk.charAt(flag-1) == "1";
    }

    public static checkGuide(type:string,cnt:any=null){
        if( Model.GuideType == type ){
            if( Model.GuideNeedValue != null)
            {
                if(Model.GuideNeedValue instanceof Array){
                    if(Model.GuideNeedValue.length == 0 || Model.GuideNeedValue.indexOf(cnt) != -1){
                        Model.GuideCall.call(Model.GuideObserver);
                    }
                }else{
                    if(Model.GuideNeedValue == undefined || Model.GuideNeedValue <= cnt){
                        Model.GuideCall.call(Model.GuideObserver);
                    }
                }
            }else{
                Model.GuideCall.call(Model.GuideObserver);
            }
        }
    }

    public static GetDecUnlockConfig(goodid):any{
        var len = Model.decUnlockConfig.length;
        var config;
        var found;
        for(var i = 0; i < len; i++){
            config = Model.decUnlockConfig[i];
            if( config.goodsId == goodid){
                found = true;
                break;
            }
        }
        return found ? config : null;
    }

    public static GetUnlockConfig(petid:number):any{
        var len = Model.UnlockConfigs.length;
        var config;
        var found;
        for(var i = 0; i < len; i++){
            config = Model.UnlockConfigs[i];
            if( config.id == petid){
                found = true;
                break;
            }
        }
        return found ? config : null;
    }
    /**
     * 基本上所有物品
     * **/
    public static GetGoodInfoById(id){
        var type = parseInt(id.toString().substr(0,3));//Math.floor也可以
        var result:GoodInfo;
        if( type == Model.GOOD_TYPE_DECORATE){
            result = Model.GetDecorateById(id);
        }else if( type == Model.GOOD_TYPE_ITEM){
            result = Model.GetGoodById(id);
        }else if( type == Model.GOOD_TYPE_MONEY){
            result = Model.GetMoenyById(id);
        }
        else if( type == Model.GOOD_TYPE__GUILD_GOODS){
            result = Model.GetGuildGoodById(id);
        }else if( type == Model.GOOD_TYPE_GUILD_MATERIAL){
            result = Model.GetGuildMaterialById(id);
        }
        if( result == null){
            result = Model.GetHiddenGoodById(id);
        }
        return result;
    }
    /**
     * 商品
     * **/
    public static GetGoodById(id:number):GoodInfo{
        var len = Model.goods.length;
        var good:GoodInfo;
        for( var idx = 0; idx < len; idx++){
            good = Model.goods[idx];
            if( good.id == id ){
                return good;
            }
        }
        return null;
    }
    /**
     * 隐藏商品商品
     * **/
    public static GetHiddenGoodById(id:number):GoodInfo{
        var len = Model.hideGoods.length;
        var good:GoodInfo;
        for( var idx = 0; idx < len; idx++){
            good = Model.hideGoods[idx];
            if( good.id == id ){
                return good;
            }
        }
        return null;
    }
    /**
     * 帮派商品
     * **/
    public static GetGuildGoodById(id:number):GoodInfo{
        var len = Model.guildGoods.length;
        var good:GoodInfo;
        for( var idx = 0; idx < len; idx++){
            good = Model.guildGoods[idx];
            if( good.id == id ){
                return good;
            }
        }
        return null;
    }
    /**
     * 帮派商品
     * **/
    public static GetGuildMaterialById(id:number):GoodInfo{
        var len = Model.guildMaterial.length;
        var good:GoodInfo;
        for( var idx = 0; idx < len; idx++){
            good = Model.guildMaterial[idx];
            if( good.id == id ){
                return good;
            }
        }
        return null;
    }
    /**
     * 帮派商品
     * **/
    //public static GetGuildCatById(id:number):GoodInfo{
    //    var len = Model.guildCat.length;
    //    var good:GoodInfo;
    //    for( var idx = 0; idx < len; idx++){
    //        good = Model.guildCat[idx];
    //        if( good.id == id ){
    //            return good;
    //        }
    //    }
    //    return null;
    //}
    /**
     * 帮派商品
     * **/
    //public static GetGuildDogById(id:number):GoodInfo{
    //    var len = Model.guildDog.length;
    //    var good:GoodInfo;
    //    for( var idx = 0; idx < len; idx++){
    //        good = Model.guildDog[idx];
    //        if( good.id == id ){
    //            return good;
    //        }
    //    }
    //    return null;
    //}
    /**
     * 货币
     * **/
    public static GetMoenyById(id:number):GoodInfo{
        var len = Model.moneys.length;
        var good:GoodInfo;
        for( var idx = 0; idx < len; idx++){
            good = Model.moneys[idx];
            if( good.id == id ){
                return good;
            }
        }
        return null;
    }

    public static GetDiamondDataId(id:number):GoodInfo{
        var len = Model.diamonds.length;
        var good:GoodInfo;
        for( var idx = 0; idx < len; idx++){
            good = Model.diamonds[idx];
            if( good.id == id ){
                return good;
            }
        }
        return null;
    }

    public static GetDecorateById(id:number):GoodInfo{
        var len = Model.decorators.length;
        var good:GoodInfo;
        for( var idx = 0; idx < len; idx++){
            good = Model.decorators[idx];
            if( good.id == id ){
                return good;
            }
        }
        return null;
    }
    public static updateDecorate(data){
        var dec = Model.DecorateOn(data.goodsId);
        if (dec){
            dec.initFromData(data);
            return;
        }
        Model.addFreeDec(data);
    }
    public static DecorateOn(did):DecorateInfo{
        var arr = Model.currentDecorators;
        if( !arr || arr.length == 0)
            return null;
        var len = arr.length;
        var d:DecorateInfo;
        for( var i = 0; i < len; i++){
            d = arr[i];
            if( d.goodsId == did){
                return d;
            }
        }
        return null;
    }
    public static addFreeDec(data){
        var len = Model.myDecorates.length;
        var d:DecorateInfo;
        for( var i = 0; i < len; i++){
            d = Model.myDecorates[i];
            if( d.goodsId == data.goodsId){
                d.initFromData(data);
                return;
            }
        }
        Model.myDecorates.push(new DecorateInfo(data));
    }
    public static DeleteDec(did):DecorateInfo{
        var arr = Model.currentDecorators;
        if( !arr || arr.length == 0)
            return;
        var len = arr.length;
        var d:DecorateInfo;
        for( var i = 0; i < len; i++){
            d = arr[i];
            if( d.goodsId == did){
                return arr.splice(i,1)[0];//fixme
            }
        }
        return null;
    }

    public static HasFreeDec(did):DecorateInfo{
        var arr = Model.myDecorates;
        if( !arr ) return null;
        var len = arr.length;
        var d:DecorateInfo;
        for( var i = 0; i < len; i++){
            d = arr[i];
            if( d.goodsId == did){
                return d;
            }
        }
        return null;
    }

    public static DeleteFreeDec(did):DecorateInfo{
        var arr = Model.myDecorates;
        var len = arr.length;
        var d:DecorateInfo;
        for( var i = 0; i < len; i++){
            d = arr[i];
            if( d.goodsId == did){
                Model.myDecorates.splice(i,1);
                return d;
            }
        }
        return null;
    }

    public static GetPerAttrs(petid):any{
        var arr = Model.petAttributes;
        var len = arr.length;
        var d;
        for( var i = 0; i < len; i++){
            d = arr[i];
            if( d.petId == petid){
                return d;
            }
        }
        return null;
    }

    public static DeleteDateItem(data:Pet){
        var len = Model.strangerList.length;
        var pet:Pet;
        for(var idx = 0; idx < len; idx++){
            pet = Model.strangerList[idx];
            if( pet.petId == data.petId && pet.userId == data.userId){
                Model.strangerList.splice(idx,1);
                break;
            }
        }
    }

    public static DeleteMailById(mailId:number):boolean{
        if(Model.curMails == undefined) return;
        var len = Model.curMails.length;
        var mailInfo:MailInfo;
        for(var idx =0; idx < len; idx++){
            mailInfo = Model.curMails[idx];
            if( mailInfo.id == mailId){
                Model.curMails.splice(idx, 1);
                Model.mailMap[mailInfo.id] = false;
                return true;
            }
        }
        return false;
    }

    public static GetPropCnt(propId){
        var len = Model.props.length;
        var propInfo:PropInfo;
        for(var idx = 0; idx < len; idx++){
            propInfo = Model.props[idx];
            if( propInfo.propId == propId){
                return propInfo.count;
            }
        }
        return 0;
    }

    public static updatePropCnt(propId,cnt){
        var len = Model.props.length;
        var got = false;
        var propInfo:PropInfo;
        for(var idx = 0; idx < len; idx++){
            propInfo = Model.props[idx];
            if( propInfo.propId == propId){
                got = true;
                propInfo.count = cnt;
                break;
            }
        }
        if( !got) {
            propInfo = new PropInfo(null);
            propInfo.count = cnt;
            propInfo.propId = propId;
            Model.props.push(propInfo);
        }
    }

    public static addPropCnt(propId,cnt){
        var len = Model.props.length;
        var got = false;
        var propInfo:PropInfo;
        for(var idx = 0; idx < len; idx++){
            propInfo = Model.props[idx];
            if( propInfo.propId == propId){
                got = true;
                propInfo.count+=cnt;
                break;
            }
        }
        if( !got) {
            propInfo = new PropInfo(null);
            propInfo.count = cnt;
            propInfo.propId = propId;
            Model.props.push(propInfo);
        }
    }

    public static setPropCnt(propId, cnt){
        var len = Model.props.length;
        var got = false;
        var propInfo:PropInfo;
        for(var idx = 0; idx < len; idx++){
            propInfo = Model.props[idx];
            if( propInfo.propId == propId){
                got = true;
                propInfo.count = cnt;
                if( cnt == 0){
                    Model.props.splice(idx,1);
                }
                break;
            }
        }
        if( !got) {
            propInfo = new PropInfo(null);
            propInfo.count = cnt;
            propInfo.propId = propId;
            Model.props.push(propInfo);
        }
    }

    public static updatePetData(petdata:any){
        var len = Model.pets.length;
        var pet:Pet;
        for(var idx = 0; idx  < len; idx++){
            pet = Model.pets[idx];
            if(pet.petId == petdata.petId){
                pet.initFromData(petdata);
                break;
            }
        }
    }
    public static getPetById(petid:number):Pet{
        var len = Model.pets.length;
        var pet:Pet;
        var result:Pet=null;
        for(var idx = 0; idx  < len; idx++){
            pet = Model.pets[idx];
            if(pet.petId == petid){
                result = pet;
                break;
            }
        }
        return result;
    }

    public static getPetInfoById(petid:number):GoodInfo{
        var len = Model.cats.length;
        var pet:GoodInfo;
        for(var idx = 0; idx  < len; idx++){
            pet = Model.cats[idx];
            if(pet.id == petid){
                return pet;
            }
        }

        len = Model.dogs.length;
        for(idx = 0; idx  < len; idx++){
            pet = Model.dogs[idx];
            if(pet.id == petid){
                return pet;
            }
        }
        return null;
    }

    public static constainsPet(petId:number):boolean{
        var len = Model.pets.length;
        var pet:Pet;
        var result:boolean=false;
        for(var idx = 0; idx  < len; idx++){
            pet = Model.pets[idx];
            if(pet.petId == petId){
                result = true;
                break;
            }
        }
        return result;
    }

    public static GetDecDateById(id):any{
        var len = Model.decModels.length;
        var result;
        var data;
        for(var idx = 0; idx  < len; idx++){
            data = Model.decModels[idx];
            if(data.id == id){
                result = data;
                break;
            }
        }
        return result;
    }

    public static clickData:Object = {};
    public static updateClickCnt(petid:number, cnt:number){
        if( Model.clickData[petid] == undefined){
            Model.clickData[petid] = cnt;
        }else{
            Model.clickData[petid] += cnt;
        }
    }
    public static TEMP_DOG_HAIRS:number = 0;
    public static TEMP_CAT_HAIRS:number = 0;
    public static produceHairData:Object = {};
    public static updateProduceHairData(petid,cnt){
        if( Model.produceHairData[petid] == undefined){
            Model.produceHairData[petid] = cnt;
        }else{
            Model.produceHairData[petid] += cnt;
        }
        var pet = Model.getPetById(petid);
        if( pet.type == Model.GOOD_TYPE_CAT){
            Model.TEMP_CAT_HAIRS += cnt;
        }else if( pet.type == Model.GOOD_TYPE_DOG){
            Model.TEMP_DOG_HAIRS += cnt;
        }
    }

    public static cleanProduceHairData(petid){
        var cnt = Model.produceHairData[petid];
        if( cnt != 0){
            var pet = Model.getPetById(petid);
            if( pet.type == Model.GOOD_TYPE_CAT){
                Model.TEMP_CAT_HAIRS -= cnt;
            }else if( pet.type == Model.GOOD_TYPE_DOG){
                Model.TEMP_DOG_HAIRS -= cnt;
            }
        }
        Model.produceHairData[petid] = 0;
    }
}